﻿/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

using System;

namespace Lucene.Net.Benchmark.ByTask.Utils {

	/**
	 * Formatting utilities (for reports).
	 */
	public class Format {

		private static string[] numFormat = new string[ 3 ] { "{0:0}", "{0:0.0}", "{0:0.00}" };
		
		private static readonly string padd = "                                                 ";
  

	  /**
	   * Padd a number from left.
	   * @param numFracDigits number of digits in fraction part - must be 0 or 1 or 2.
	   * @param f number to be formatted.
	   * @param col column name (used for deciding on length).
	   * @return formatted string.
	   */
	  public static String format(int numFracDigits, float f, String col) {
		  string res = padd + String.Format( numFormat[ numFracDigits ], f );
		  return res.Substring(res.Length - col.Length);
	  }

	  public static String format(int numFracDigits, double f, String col) {
		  string res = padd + String.Format( numFormat[ numFracDigits ], f );
		  return res.Substring( res.Length - col.Length );
	  }

	  /**
	   * Pad a number from right.
	   * @param numFracDigits number of digits in fraction part - must be 0 or 1 or 2.
	   * @param f number to be formatted.
	   * @param col column name (used for deciding on length).
	   * @return formatted string.
	   */
	  public static String formatPaddRight(int numFracDigits, float f, String col) {
		String res = String.Format(numFormat[numFracDigits], f) + padd;
		return res.Substring(0, col.Length);
	  }

	  public static String formatPaddRight(int numFracDigits, double f, String col) {
		  String res = String.Format( numFormat[ numFracDigits ], f ) + padd;
		  return res.Substring( 0, col.Length );
	  }

	  /**
	   * Pad a number from left.
	   * @param n number to be formatted.
	   * @param col column name (used for deciding on length).
	   * @return formatted string.
	   */
	  public static String format(int n, String col) {
		String res = padd + n;
		return res.Substring(res.Length - col.Length);
	  }

	  /**
	   * Pad a string from right.
	   * @param s string to be formatted.
	   * @param col column name (used for deciding on length).
	   * @return formatted string.
	   */
	  public static String format(String s, String col) {
		String s1 = (s + padd);
		return s1.Substring(0, Math.Min(col.Length, s1.Length));
	  }

	  /**
	   * Pad a string from left.
	   * @param s string to be formatted.
	   * @param col column name (used for deciding on length).
	   * @return formatted string.
	   */
	  public static String formatPaddLeft(String s, String col) {
		String res = padd + s;
		return res.Substring(res.Length - col.Length);
	  }

	  /**
	   * Extract simple class name 
	   * @param cls class whose simple name is required 
	   * @return simple class name
	   */
	  public static String simpleName (Type cls) {
		String c = cls.Name;
		String p = cls.AssemblyQualifiedName;
		int k = c.LastIndexOf(p+".");
		if (k<0) {
		  return c;
		}
		return c.Substring(k+1+p.Length);
	  }

	} // end of class
}
